package net.tycmc.cty.ctyms.web.Action;

import java.io.IOException;
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


import net.sf.json.JSONArray;
import net.tycmc.bulb.common.util.StringUtil;
import net.tycmc.bulb.dbutil.pager.Pager;
import net.tycmc.bulb.dbutil.pager.PagerModel;
import net.tycmc.cty.ctyms.web.Service.DBManagePlatFormService;

public class DBManagePlatFormAction extends BaseActionS2{
	private DBManagePlatFormService dbmpfs;
	
	public DBManagePlatFormService getDbmpfs() {
		return dbmpfs;
	}

	public void setDbmpfs(DBManagePlatFormService dbmpfs) {
		this.dbmpfs = dbmpfs;
	}

	/**
	 * 初始化方法，用于跳转
	 * @return
	 */
	public String DBManagePlatForm(){
		return "DBManagePlatFormInit";
	}
	
	/**
	 * 获取列表数据
	 * @return
	 * @throws IOException 
	 */
	public String getlist() throws IOException{
 		String pageNum=request.getParameter("page");
		String pagelength=request.getParameter("pagelength");
		String total=request.getParameter("total");
		if(null==pageNum){
			pageNum="1";
		}
		int realPageNum=Integer.parseInt(pageNum);
		int pagelengths=Integer.parseInt(pagelength);
		Pager page = new Pager();
		page.setStart((realPageNum-1)*pagelengths);
		page.setLength(pagelengths);
		int totalrecord=0;
		String flag="true";
		if(null==total||"".equals(total)){
			flag="false";
		}
		PagerModel pm=dbmpfs.getDBList(page,flag);
		if(flag.equals("false")){
			totalrecord=pm.getTotal();
		}else{
			totalrecord=Integer.parseInt(total);
		}
		JSONArray json=JSONArray.fromObject(pm.getDatas());
		response.setContentType("text/html;charset=gbk");
		PrintWriter out = response.getWriter();
		out.print("{'total':"+totalrecord+",'data':"+json.toString()+"}");
		return null;
	}
	/**
	 * 修改
	 * @return
	 */
	public String DbInfoEdit() {
		String id=request.getParameter("id");
		String ip=request.getParameter("ip");
		String port=request.getParameter("port");
		String dbservername=request.getParameter("dbservername");
		String username=request.getParameter("username");
		String password=request.getParameter("password");
		String type=request.getParameter("type");
		String datemonth=request.getParameter("datemonth");
		String dbname=request.getParameter("dbname");
		String remark=request.getParameter("remark");
		String oprtname=request.getParameter("oprtname");
		String eff=request.getParameter("eff");
		String exp=request.getParameter("exp");
		String dbslongname=request.getParameter("dbslongname");
		request.setAttribute("id", id);
		request.setAttribute("ip", ip);
		request.setAttribute("port", port);
		request.setAttribute("dbservername", dbservername);
		request.setAttribute("username", username);
		request.setAttribute("password", password);
		request.setAttribute("type", type);
		if("null".equals(datemonth)||!StringUtil.isValid(datemonth)){
			datemonth="";
		}
		if("null".equals(dbslongname)||!StringUtil.isValid(dbslongname)){
			dbslongname="";
		}
		request.setAttribute("datemonth", datemonth);
		
		if("null".equals(oprtname)||!StringUtil.isValid(oprtname)){
			oprtname="";
		}
		if("null".equals(remark)||!StringUtil.isValid(remark)){
			remark="";
		}
		request.setAttribute("dbname", dbname);
		request.setAttribute("remark", remark);
		request.setAttribute("oprtname", oprtname);
		request.setAttribute("eff", eff);
		request.setAttribute("exp", exp);
		request.setAttribute("dbslongname",dbslongname);
		return "DBManagePlatFormEdit";
	}
	
	/**
	 * 修改
	 * @return
	 * @throws IOException 
	 */
	public String DbInfoEdit_OK() throws IOException {
		String id=request.getParameter("id");
		String ip=request.getParameter("ip");
		String port=request.getParameter("port");
		String dbservername=request.getParameter("dbservername");
		String dbslongname=request.getParameter("dbslongname");
		String username=request.getParameter("username");
		String password=request.getParameter("password");
		String type=request.getParameter("type");
		String datemonth=request.getParameter("datemonth");
		String dbname=request.getParameter("dbname");
		String remark=request.getParameter("remark");
		String oprtname=request.getParameter("oprtname");
		String eff=request.getParameter("eff");
		String exp=request.getParameter("exp");
		Map<String,Object>map=new HashMap<String, Object>();
		map.put("id", id);
		map.put("ip", ip);
		map.put("port", port);
		map.put("dbservername", dbservername);
		map.put("username", username);
		map.put("password", password);
		map.put("type", type);
		map.put("datemonth", datemonth);
		map.put("dbname", dbname);
		map.put("remark", remark);
		map.put("oprtname", oprtname);
		map.put("eff", eff);
		map.put("exp", exp);
		map.put("dbslongname",dbslongname);
		int num=dbmpfs.updateDbInfo(map);
		PrintWriter out = response.getWriter();
		if(num>=1){
			out.print("<script language='javascript'>");
			out.print("alert('修改成功');");
			out.print("window.location.href='DBManagePlatFormAction!DBManagePlatForm'");
			out.print("</script>");
		}else{
			out.print("<script language='javascript'>");
			out.print("alert('修改失败');");
			out.print("window.location.href='DBManagePlatFormAction!DBManagePlatForm'");
			out.print("</script>");
		}
		return null;
	}
	/**
	 * 删除
	 * @return
	 * @throws IOException 
	 */
	public String DbInfoDelete() throws IOException {
		String id=request.getParameter("id");
		request.setAttribute("id", id);
		return "DBManagePlatFormDelete";
	}
	/**
	 * 删除
	 * @return
	 * @throws IOException 
	 */
	public String DbInfoDelete_OK() throws IOException {
		String id=request.getParameter("id");
		String OprtName =request.getParameter("OprtName");
		String remark=request.getParameter("remark");
		int num=dbmpfs.deleteDbInfo(id,OprtName,remark);
		PrintWriter out = response.getWriter();
		if(num>=1){
			out.print("<script language='javascript'>");
			out.print("alert('删除成功');");
			out.print("window.location.href='DBManagePlatFormAction!DBManagePlatForm'");
			out.print("</script>");
		}else{
			out.print("<script language='javascript'>");
			out.print("alert('删除失败');");
			out.print("window.location.href='window.history.go(-1)'");
			out.print("</script>");
		}
		return null;
	}
	
	public String DbInfoAdd() throws IOException {
		return "DBManagePlatFormAdd";
	}
	
	/**
	 * 新增
	 * @return
	 * @throws IOException 
	 */
	public String DbInfoAdd_OK() throws IOException {
		String id=request.getParameter("id");
		String ip=request.getParameter("ip");
		String port=request.getParameter("port");
		String dbservername=request.getParameter("dbservername");
		String username=request.getParameter("username");
		String password=request.getParameter("password");
		String type=request.getParameter("type");
		String datemonth=request.getParameter("datemonth");
		String dbname=request.getParameter("dbname");
		String remark=request.getParameter("remark");
		String oprtname=request.getParameter("oprtname");
		String eff=request.getParameter("eff");
		String exp=request.getParameter("exp");
		String dbslongname=request.getParameter("dbslongname");
		Map<String,Object>map=new HashMap<String, Object>();
		map.put("id", id);
		map.put("ip", ip);
		map.put("port", port);
		map.put("dbservername", dbservername);
		map.put("username", username);
		map.put("password", password);
		map.put("type", type);
		map.put("datemonth", datemonth);
		map.put("dbname", dbname);
		map.put("remark", remark);
		map.put("oprtname", oprtname);
		map.put("eff", eff);
		map.put("exp", exp);
		map.put("dbslongname",dbslongname);
		int num=dbmpfs.AddDbInfo(map);
		PrintWriter out = response.getWriter();
		if(num>=1){
			out.print("<script language='javascript'>");
			out.print("alert('新增成功');");
			out.print("window.location.href='DBManagePlatFormAction!DBManagePlatForm'");
			out.print("</script>");
		}else{
			out.print("<script language='javascript'>");
			out.print("alert('新增失败');");
			out.print("window.location.href='window.history.go(-1)'");
			out.print("</script>");
		}
		return null;
	}
}
